package org.winexpress

import grails.converters.deep.JSON
import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

class AdminAccountController {

    // initialization: GSP support operations (must be same as GSP filename)
    def accountStatusActivation =
    {
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/customer").text
        
        JSONArray arr  = new JSONArray(txt)
        ArrayList<Customer> customerList = new ArrayList<Customer>()
    
        arr.each()
        {
           Customer c = new Customer()
           c.id = it.id
           println 'accountStatusActivation: ' + c.id
           c.username = it.username.toString()
           println ': ' + c.username
           c.password = it.password.toString()
           println ': ' + c.password
           c.lastName = it.lastName.toString()
           println ': ' + c.lastName
           c.firstName = it.firstName.toString()
           println ': ' + c.firstName
           c.activationStatus = it.activationStatus
           println ': ' + c.activationStatus
           customerList.add(c)
           println ': ' + c
        }
//        customerList.each()
//        {
//            println it
//        }
        
        [customers:customerList]
    }
    
    def accountStatusActivationDetail = {
        // get form values
        def id = params['id']
        println id
        def username = params['username']
        println username
        def lastName = params['lastName']
        println lastName
        def firstName = params['firstName']
        println firstName
        def activationStatus = params['activationStatus']
        println activationStatus
        
        [customerId:id, customerUsername:username, customerLastName:lastName, customerFirstName:firstName, customerActivationStatus:activationStatus]
    }
    
    def accountClosure = {
        def customerId = session.customerId
        
        
        // consume winexpress server web service
        def txt = new URL("http://localhost:8080/WineXpress/country").text
        
        JSONArray arr  = new JSONArray(txt)
        ArrayList<Country> countryList = new ArrayList<Country>()
    
        arr.each()
        {
           Country c = new Country()
           c.countryName = it.countryName
           c.id = it.id
           countryList.add(c)
        }
        
        
        // consume winexpress server web service
        def txtCustomer = new URL("http://localhost:8080/WineXpress/customer/" + customerId).text
        
        // todo: either package as customer or convert only birthdate
        JSONObject objCustomer = new JSONObject(txtCustomer)
        println "editAccountInformation >> Customer Retrieved: " + objCustomer.id + " " + objCustomer.lastName + " " + objCustomer.firstName
        IntRange indexRange = new IntRange(0,9)
        def ll = objCustomer.lastLogin.getAt(indexRange)
        def bd = objCustomer.birthdate.getAt(indexRange)
        
        // consume winexpress server web service
        def txtCountry = new URL("http://localhost:8080/WineXpress/country/" + objCustomer.country.id).text
        
        JSONObject objCountry = new JSONObject(txtCountry)
        def countryName = objCountry.countryName

        println "editAccountInformation OK"
        
        [customer:objCustomer, countryName:countryName, lastLogin:ll, birthdate:bd, countries:countryList]
    }
    
    
    
    
    // REST-related operations for accessing WineXpress Server (for clearer view only)
    // GET
    def show = {
        
    }
    
    // PUT
    def updateAccountStatus = {
        // get form values
        def id = params['id']
        def username = params['username']
        def activationStatus = params.activationStatus
        
        // create a JSONObject to send to backend
        JSONObject o = new JSONObject()
        o.put("id",id)
        o.put("activationStatus",activationStatus)
        
        // to create use PUT
        HttpClient httpclient = new DefaultHttpClient()
        HttpResponse response
        // consume winexpress server web service
        HttpPut httpput = new HttpPut("http://localhost:8080/WineXpress/customer") // WineXpress Server's URI
        StringEntity se = new StringEntity(o.toString())     
        httpput.setEntity(se);          
        response = httpclient.execute(httpput);
        
        flash.message = "Member account status for " + username + " updated."
        
        redirect(uri:"/accountStatusActivation")
    }
    
    // DELETE
    def delete = {
        
    }
    
    // POST
    def postCreateMember = {
        // get form values
//        def username = params['username']        
//        def password = params['password']
//        def lastName = params['lastName']        
//        def firstName = params['firstName']
//        def birthdate = params['birthdate']
//        def blkHouseNo = params['birthdate']
//        def streetName = params['streetName']
//        def country = params['country']
//        def postalCode = params.postalCode
//        def telNo = params.telNo
//        
//        // create a JSONObject to send to backend
//        JSONObject o = new JSONObject()
//        o.put("userName",username)
//        o.put("password",password)
//        o.put("lastName",lastName)
//        o.put("firstName",firstName)
//        o.put("birthdate",birthdate)
//        o.put("blkHouseNo",blkHouseNo)
//        o.put("streetName",streetName)
//        o.put("country",country)
//        o.put("postalCode",postalCode)
//        o.put("telNo",telNo)
//        
//        // to create use POST
//        HttpClient httpclient = new DefaultHttpClient()
//        HttpResponse response
//        // consume winexpress server web service
//        HttpPost httppost = new HttpPost("http://localhost:8080/WineXpress/customer") // WineXpress Server's URI
//        StringEntity se = new StringEntity(o.toString())          
//        httppost.setEntity(se);          
//        response = httpclient.execute(httppost);

//        redirect(uri:"/thankyou")
    }
}
